package org.tio.sitexxx.web.server.utils;

import net.sf.json.JSONObject;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.tio.utils.jfinal.P;

import java.io.IOException;

public class WxConsultantLogin {
    private static WxProperties wxConfig;
    public static JSONObject getSessionKeyOrOpenId(String code) throws IOException {
        //微信端登录code
        System.out.println(P.get("wxpay.config.consultant.appId")+P.get("wxpay.config.consultant.secret"));
        String wxCode = "https://api.weixin.qq.com/sns/jscode2session?" +
                "grant_type=authorization_code" +
                "&appid=" + P.get("wxpay.config.consultant.appId") +
                "&secret=" + P.get("wxpay.config.consultant.secret") +
                "&js_code=" + code;
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder().url(wxCode).build();
        //发送post请求读取调用微信接口获取openid用户唯一标识
        Response response = client.newCall(request).execute();
        //获取请求内容
        String content = response.body().string();
        System.out.println(content+"--------------content");
        JSONObject jsonObject = JSONObject.fromObject(content);
        return jsonObject;
    }

    public  static  JSONObject getUserInfo(String encryptedData, String sessionKey, String iv)throws Exception{
        String result = AesCbcUtil.decrypt(encryptedData, sessionKey, iv, "UTF-8");
        //位于util包下的工具类
        JSONObject resultJson = JSONObject.fromObject(result);
        return resultJson;
    }
}
